package com.ciaojian.core.bytedance;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ciaojian.core.util.LogUtil;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class DySubscribeApi {
    public static HttpResponse subscribeMessageSend(JSONObject param, String subscribeNotification,String url) {

        try {
            HttpPost httpPost = new HttpPost(subscribeNotification);
            CloseableHttpClient client = HttpClients.createDefault();
            StringEntity entity = new StringEntity(param.toString(), "UTF-8");

            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");
            httpPost.setEntity(entity);

            long timestamp = System.currentTimeMillis() / 1000;

            String signature = ByteDanceSignUtil.getSignature("POST", url, timestamp, param.toJSONString());

            String sign = " " +
                    "" + ByteDanceMiniConfig.BYTE_AUTHORIZATION + " " +
                    "appid=\"" + ByteDanceMiniConfig.APP_ID + "\"," +
                    "nonce_str=\"" + ByteDanceMiniConfig.NONCE_STR + "\"," +
                    "timestamp=\"" + timestamp + "\"," +
                    "key_version=\"1\"," +
                    "signature=\"" + signature + "\"";

            httpPost.setHeader("Byte-Authorization", sign);
            httpPost.setHeader("Content-Type", "application/json");


            HttpResponse response = client.execute(httpPost);

            return response;
        }catch (Exception e){

        }
        return null;
    }
}
